<template>
  <div class="mod-config">
    <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList()">
      <el-form-item>
        <el-input v-model="dataForm.keywords" placeholder="商品名称" clearable></el-input>
      </el-form-item>
      <el-form-item label="">
				<el-date-picker
          v-model="dataForm.startTime"
          type="datetime"
          value-format = "yyyy-MM-dd HH:mm:ss"
          placeholder="起始时间">
			  </el-date-picker>
			</el-form-item>
			<el-form-item label="">
				<el-date-picker
          v-model="dataForm.endTime"
          type="datetime"
          value-format = "yyyy-MM-dd HH:mm:ss"
          placeholder="结束时间">
			  </el-date-picker>
			</el-form-item>
      <el-form-item label="店铺分类">
		<el-select v-model="dataForm.userClass" placeholder="店铺分类">
			<el-option v-for="item in this.goodsClass" :key="item.userId" :label="item.className" :value="item.userId"></el-option>
		</el-select>
	  </el-form-item>
      <el-form-item label="商品类型">
		<el-select v-model="dataForm.goodsType" placeholder="商品类型">
			<el-option label="全部" value=""></el-option>
			<el-option label="自营商品" value="0"></el-option>
			<el-option label="兑换商品" value="3"></el-option>
			<el-option label="新人专享" value="4"></el-option>
	    </el-select>
	  </el-form-item>
      <el-form-item>
        <el-button @click="
          dataForm.keywords = ''
          dataForm.startTime = ''
          dataForm.endTime = ''
          dataForm.userClass = ''
          dataForm.goodsType = ''
          getDataList()">重置</el-button>
        <el-button @click="getDataList()">查询</el-button>
        <el-button v-if="isAuth('business:goods:addPrefabricatedGoods')" type="primary" @click="addOrUpdateHandle()">新增</el-button>
        <el-form-item label="">
        <el-select v-model="batchValue" placeholder="批量操作" v-if="isAuth('business:goods:modifyBatchPrefabricatedGoodsById')">
          <el-option label="批量下架" value="batchLowShelf"></el-option>
          <el-option label="批量上架" value="batchUpShelf"></el-option>
          <el-option label="批量删除" value="batchDelete"></el-option>
          <el-option label="批量修改分类" value="batchUserClass"></el-option>
        </el-select>
        <el-select v-model="userclass" placeholder="选择分类" v-if="batchValue !== '' && batchValue === 'batchUserClass'">
            <el-option label="生鲜水果" value="373"></el-option>
            <el-option label="美食饮品" value="374"></el-option>
            <el-option label="生活服务" value="375"></el-option>
            <el-option label="丽人美妆" value="376"></el-option>
            <el-option label="家具建材" value="377"></el-option>
            <el-option label="超市便利" value="378"></el-option>
            <el-option label="休闲娱乐" value="381"></el-option>
        </el-select>
        <el-button v-if="isAuth('business:goods:modifyBatchPrefabricatedGoodsById')" type="primary" plain @click="batch(batchValue, userclass)" :disabled="batchValue===''">批量操作</el-button>
      </el-form-item>
        <!-- <el-button v-if="isAuth('goods:shoppingbusinessgoods:delete')" type="danger" @click="deleteHandle()" :disabled="dataListSelections.length <= 0">批量删除</el-button> -->
      </el-form-item>
    </el-form>
    <el-table
      :data="dataList"
      border
      v-loading="dataListLoading"
      @selection-change="selectionChangeHandle"
      style="width: 100%;">
      <el-table-column
        type="selection"
        header-align="center"
        align="center"
        width="50">
      </el-table-column>
      <el-table-column
        prop="goodsId"
        header-align="center"
        align="center"
        label="商品编号">
      </el-table-column>
      <el-table-column
        prop="typeId"
        header-align="center"
        align="center"
        label="商品类型">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.typeId === 0" type="warning">自营商品</el-tag>
          <el-tag v-if="scope.row.typeId === 3" type="success">兑换商品</el-tag>
          <el-tag v-if="scope.row.typeId === 4">新人专享</el-tag>
        </template>
      </el-table-column>
      <el-table-column
        prop="userId"
        header-align="center"
        align="center"
        label="商品分类">
        <template slot-scope="scope">
          <!-- <el-tag v-if="scope.row.userId === 373">生鲜水果</el-tag>
          <el-tag v-if="scope.row.userId === 374">美食饮品</el-tag>
          <el-tag v-if="scope.row.userId === 375">生活服务</el-tag>
          <el-tag v-if="scope.row.userId === 376">丽人美妆</el-tag>
          <el-tag v-if="scope.row.userId === 377">家具建材</el-tag>
          <el-tag v-if="scope.row.userId === 378">超市便利</el-tag>-->
          <el-tag>
            {{goodsClass.filter(c=>{
              return c.userId === scope.row.userId
              })[0].className}}
            </el-tag>
          <!-- <el-tag v-for="item in goodsClass" :key="item.id" :v-if="scope.row.userId === item.id">{{item.className}}</el-tag> -->
        </template>
      </el-table-column>
      <el-table-column
        prop="goodsName"
        header-align="center"
        align="center"
        label="商品名称">
      </el-table-column>
      <el-table-column
        prop="goodsImg"
        header-align="center"
        align="center"
        label="商品图片">
        <template slot-scope="scope">
          <el-popover
            placement="right"
            title=""
            trigger="hover">
            <img :src="'http://image.yilianbaihui.cn/consumer/goods/' + scope.row.goodsImg"/>
            <img slot="reference" :src="'http://image.yilianbaihui.cn/consumer/goods/' + scope.row.goodsImg" :alt="'http://image.yilianbaihui.cn/consumer/goods/' + scope.row.goodsImg" style="max-height: 50px;max-width: 130px">
          </el-popover>
        </template>
      </el-table-column>
      <el-table-column
        prop="marketPrice"
        header-align="center"
        align="center"
        label="市场价">
        <template slot-scope="scope">
          <span v-if="scope.row.marketPrice != null">￥ {{scope.row.marketPrice}}</span>
          <span v-if="scope.row.marketPrice == null">￥ 0</span>
        </template>
      </el-table-column>
      <el-table-column
        prop="vipPrice"
        header-align="center"
        align="center"
        label="推荐售价">
        <template slot-scope="scope">
          <span v-if="scope.row.vipPrice != null">￥ {{scope.row.vipPrice}}</span>
          <span v-if="scope.row.vipPrice == null">￥ 0</span>
        </template>
      </el-table-column>
      <el-table-column
        prop="goodsStock"
        header-align="center"
        align="center"
        label="库存">
      </el-table-column>
      <el-table-column
        prop="isSale"
        header-align="center"
        align="center"
        label="上下架">
        <template slot-scope="scope">
          <el-switch
            style="margin-left: 10px;margin-bottom: 5px;"
            v-model="scope.row.isSale"
            active-value= 1
            inactive-value= 0
            @change="batchs(scope.row.goodsId, scope.row.isSale)">
          </el-switch>
        </template>
      </el-table-column>
      <el-table-column
        prop="goodsDesc"
        header-align="center"
        align="center"
        label="描述"
        show-overflow-tooltip>
      </el-table-column>
      <el-table-column
        prop="orderNum"
        header-align="center"
        align="center"
        label="排序">
        <template slot-scope="scope">
          <el-input v-model="scope.row.orderNum" type="number" @change="updateOrderNum(scope.row.goodsId, scope.row.orderNum)"/>
        </template>
      </el-table-column>
      <el-table-column
        fixed="right"
        header-align="center"
        align="center"
        width="150"
        label="操作">
        <template slot-scope="scope">
          <el-button type="text" size="small" @click="addOrUpdateHandle(scope.row.goodsId)">查看</el-button>
          <el-button type="text" size="small" @click="deleteHandle(scope.row.goodsId)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageIndex"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="pageSize"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper">
    </el-pagination>
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></add-or-update>
  </div>
</template>

<script>
  import AddOrUpdate from './prefabricated-goods-add-or-update'
  export default {
    data () {
      return {
        batchValue: '',
        userclass: 0,
        goodsClass: [],
        dataForm: {
          keywords: '',
          lastStart: '',
          lastEnd: '',
          userClass: '',
          goodsType: ''
        },
        dataList: [],
        pageIndex: 1,
        pageSize: 10,
        totalPage: 0,
        dataListLoading: false,
        dataListSelections: [],
        addOrUpdateVisible: false
      }
    },
    components: {
      AddOrUpdate
    },
    activated () {
      this.getDataList()
    },
    methods: {
      // 获取数据列表
      getDataList () {
        this.dataListLoading = true

        this.$http({
          url: this.$http.adornUrl('/business/goods/getPrefabricatedGoodsClass'),
          method: 'get',
          params: this.$http.adornParams()
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.goodsClass = data.data
          }
        })

        this.$http({
          url: this.$http.adornUrl('/business/goods/queryPrefabricatedGoods'),
          method: 'get',
          params: this.$http.adornParams({
            'page': this.pageIndex,
            'limit': this.pageSize,
            'keywords': this.dataForm.keywords,
            'lastStart': this.dataForm.lastStart,
            'lastEnd': this.dataForm.lastEnd,
            'userClass': this.dataForm.userClass,
            'goodsType': this.dataForm.goodsType
          })
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.dataList = data.data.list
            this.totalPage = data.data.totalCount
          } else {
            this.dataList = []
            this.totalPage = 0
          }
          this.dataListLoading = false
        })
      },
      // 每页数
      sizeChangeHandle (val) {
        this.pageSize = val
        this.pageIndex = 1
        this.getDataList()
      },
      // 当前页
      currentChangeHandle (val) {
        this.pageIndex = val
        this.getDataList()
      },
      // 多选
      selectionChangeHandle (val) {
        this.dataListSelections = val
      },
      // 新增 / 修改
      addOrUpdateHandle (id) {
        this.addOrUpdateVisible = true
        this.$nextTick(() => {
          this.$refs.addOrUpdate.init(id)
        })
      },
      // 删除
      deleteHandle (id) {
        let batch = 'batchDelete'
        let userClass = 0
        var ids = id ? [id] : this.dataListSelections.map(item => {
          return item.goodsid
        })
        this.$confirm(`确定对[id=${ids.join(',')}]进行删除操作?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$http({
            url: this.$http.adornUrl(`/business/goods/modifyBatchPrefabricatedGoodsById/${batch}/${userClass}`),
            method: 'post',
            data: this.$http.adornData(ids, false)
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.getDataList()
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        })
      },
      batch (batch, userClass) {
        var ids = this.dataListSelections.map(item => {
          return item.goodsId
        })
        if (ids.length === 0) {
          this.$message.error('请选择数据')
          return
        }
        if (batch === 'batchUserClass' && userClass === 0) {
          this.$message.error('请选择修改的分类类型')
          return
        }
        this.$confirm(`确定对[id=${ids.join(',')}]进行批量操作?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$http({
            url: this.$http.adornUrl(`/business/goods/modifyBatchPrefabricatedGoodsById/${batch}/${userClass}`),
            method: 'post',
            data: this.$http.adornData(ids, false)
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.getDataList()
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        })
      },
      batchs (goodsId, isSale) {
        this.$http({
          url: this.$http.adornUrl(`/business/goods/modifyIsSaleByGoodsId/${isSale}`),
          method: 'post',
          data: this.$http.adornData(goodsId, false)
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.$message({
              message: '操作成功',
              type: 'success',
              duration: 1500,
              onClose: () => {
                this.getDataList()
              }
            })
          } else {
            this.$message.error(data.msg)
          }
        })
      },
      // 修改排序
      updateOrderNum (goodsId, orderNum) {
        this.$http({
          url: this.$http.adornUrl(`/business/goods/modifyGoodsOrderNum/${goodsId}/${orderNum}`),
          method: 'post',
          data: this.$http.adornData()
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.$message({
              message: '操作成功',
              type: 'success',
              duration: 1500,
              onClose: () => {
                this.getDataList()
              }
            })
          } else {
            this.$message.error(data.msg)
          }
        })
      }
    }
  }
</script>
